Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Polymorphism in java

Polymorphism types

Method overloading in Java allows us to have multiple methods with the same name but with different argument lists. This is a form of static polymorphism (compile-time polymorphism) which helps us to perform a single task in different ways. Here are different ways to perform method overloading: Changing the Number of Parameters Method overloading can be achieved by changing the number of parameters while passing to different methods. Here’s an example:
Method overloading example in java class Product { public int multiply(int a, int b) { int prod = a * b; return prod; } public int multiply(int a, int b, int c) { int prod = a * b * c; return prod; } } class Main { public static void main(String[] args) { Product ob = new Product(); int prod1 = ob.multiply(1, 2); System.out.println("Product of the two integer value :" + prod1); int prod2 = ob.multiply(1, 2, 3); System.out.println("Product of the three integer value :" + prod2); } }

Output

Product of the two integer value :2 Product of the three integer value :6
Changing Data Types of the Arguments In many cases, methods can be considered overloaded if they have the same name but have different parameter types. Here’s an example:
Method overloading example with different datatypes of argument in java class Product { public int Prod(int a, int b, int c) { int prod1 = a * b * c; return prod1; } public double Prod(double a, double b, double c) { double prod2 = a * b * c; return prod2; } } class Main { public static void main(String[] args) { Product obj = new Product(); int prod1 = obj.Prod(1, 2, 3); System.out.println("Product of the three integer value :" + prod1); double prod2 = obj.Prod(1.0, 2.0, 3.0); System.out.println("Product of the three double value :" + prod2); } }

Output

Product of the three integer value :6 Product of the three double value :6.0
Method overriding in Java is when a subclass provides a specific implementation for a method that is already defined in its superclass. It enables us to customize the behavior of a method in the subclass while maintaining the same method signature as in the superclass. Here’s an example:
Method overriding example in java class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Bike extends Vehicle { void run() { System.out.println("Bike is running safely"); } } class Main { public static void main(String[] args) { Bike obj = new Bike(); obj.run(); } }

Output

Bike is running safely
In this example, the run method in the Bike class overrides the run method in the Vehicle class. These two concepts are fundamental to Java’s OOP and are used extensively in Java programming. They enhance the flexibility and reusability of code, and allow us to design more efficient and effective programs.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★ Polymorphism ★ method overloading ★ method overriding

Tutorials